package com.atguigu.gulimall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.common.utils.PageUtils;
import com.atguigu.gulimall.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author wangliang
 * @email wangliang@gmail.com
 * @date 2023-11-07 22:54:21
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private BrandService brandService;

    /**
     * 获取分类和品牌关联的所有品牌信息
     * Controller：用来处理请求，接受和校验数据
     * Service:接受Controller传来的数据，进行业务处理
     * Controller接受Service处理完的数据，封装成页面指定的vo
     * @return
     */
//    @RequestMapping(value = "/brands/list/{catId}",method = RequestMethod.GET)
    @RequestMapping(value = "/brands/list",method = RequestMethod.GET)
    // @RequestParam(value = "catId") 从请求参数中获取信息
    public R getCategoryBrandRelationList(@RequestParam(value = "catId") Long catId){
        List<BrandEntity> brandEntityList =  brandService.getCategoryBrandRelationList(catId);
        List<BrandVo> voList = new ArrayList<>();
        if (brandEntityList != null && brandEntityList.size() > 0){
            for (BrandEntity brandEntity : brandEntityList) {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(brandEntity.getBrandId());
                brandVo.setBrandName(brandEntity.getName());
                voList.add(brandVo);
            }
        }
        return R.ok().put("data",voList);
    }



    //@RequestMapping(path = "/catelog/list",method = RequestMethod.GET)
    @GetMapping("/catelog/list")
    public R getRelationList(@RequestParam Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(wrapper);
        return R.ok().put("data", data);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
